package org.emet.service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.emet.Constants;
import org.emet.dao.VehicleDao;
import org.emet.entity.biz.Carriage;
import org.emet.entity.biz.Vehicle;

public class VehicleStatusUpdateHelper {

	//Set ordered vehicle status to busy.
	public static void change2BusyStatus(List<Carriage> result, VehicleDao dao) {
		Iterator<Carriage> it = result.iterator();
		Date now = new Date();
		while (it.hasNext()) {
			Carriage c = it.next();
			Vehicle v = dao.getByLpn(c.getVehLPN());
			change2BusyStatus(now, c, v);
		}

	}

	public static void change2BusyStatus(Carriage c, Vehicle v) {
		Date now = new Date();
		change2BusyStatus(now, c, v);
	}

	private static void change2BusyStatus(Date now, Carriage c, Vehicle v) {
		if (now.after(c.getDate())) {
			if (v.getStatus().equals(Constants.STATUS_ORDERED)) {
				v.setStatus(Constants.STATUS_BUSY);
			}
		}
	}

	public static void change2IdleStatus(Vehicle vehicle) {
		if (vehicle.getStatus().equals(Constants.STATUS_BUSY)) {
			vehicle.setStatus(Constants.STATUS_OK);
		}
	}

}
